
$(function(){
	
	$("a[href='#chaxun']").click(function(){
		$(".sosuo input").val("");
		$(".tsbgul").html("");
		$(".shijilieb.tsbgul").css("background","#fff");
		$(".sosuo input").focus();
	});
	
	 $(".guanbixx").on("click",
     	function(){
     		$(this).hide();
//     		$(".baisek").hide();
//			$(".tsbgul").hide();
//			$.ui.scrollToTop("#chaxun");
			$(".sosuo input").val("");
     	}
     );
	 
	loadKinds();
	
	$(".zhezhao .xz_shuji li a").click(function(){
		indexPage=1;
		$(".xf_xuanze li:eq(0) a").html($(this).html());
		$(".zhezhao .xz_shuji li a").removeClass("cur");
		$(this).addClass("cur");
		loadReleaseByCondition(indexPage);
	});
	$(".zhezhao .xz_juli li a").click(function(){
		indexPage=1;
		$(".xf_xuanze li:eq(3) a").html($(this).html());
		$(".zhezhao .xz_juli li a").removeClass("cur");
		$(this).addClass("cur");
		loadReleaseByCondition(indexPage);
	});
//	loadReleaseByCondition(indexPage);
	$(".sscx_an").click(function(){
		loadReleaseByKeyWord();
	});
});
function loadReleaseByCondition(currentPage){
	var district = $(".zhezhao .xz_diqu li a.cur").html();
	var class0 = $(".zhezhao .xz_fenlei li a.cur").html();
	var canBorrow = $(".zhezhao .xz_shuji li a.cur").attr("canBorrow");
	var distance= $(".zhezhao .xz_juli li a.cur").attr("distance");
	
	$.ajax({
		url :interfaceMobileUrl+"/getUdls.json?userid="+userid+"&key="+userkey,
		jsonp: "callback",
		dataType: "jsonp",
		data:{
			city:city,
			district:district,
			class0:class0,
			distance:distance,
			canBorrow:canBorrow,
			currentPage:currentPage
		},
		beforeSend:function(){
			if(indexPage==1){
				$("#udlsList li.removable").remove();
				$("#udlsList").append(getLoadingLi("加载中..."));
			}
		},
		timeout:10000,
		error:function(XMLHttpRequest, textStatus, errorThrown){
			if(myScroll!=null){
				myScroll.refresh();
			}
			$("#udlsList li.removable").remove();
			$("#udlsList").append(getNoneDataLi("请求超时，下拉重新加载"));
		},
		success:function(data){
			if(data=="-1"||data=="-2"){
				$("#udlsList li.removable").remove();
				xfAlert("此功能要登录后才能使用");
				$(".zhezhao .xz_shuji li a").removeClass("cur");
				$(".zhezhao .xz_shuji li a:eq(0)").addClass("cur");
				$(".zhezhao .xz_juli li a").removeClass("cur");
				$(".zhezhao .xz_juli li a:eq(0)").addClass("cur");
				$(".xf_xuanze li:eq(0) a").html("全部书籍");
				$(".xf_xuanze li:eq(3) a").html("不限距离");
				loadReleaseByCondition(indexPage);
				return false;
			}
			if(data=="-4"){
				$("#udlsList li.removable").remove();
				xfAlert("此功能要绑定读者证后才能使用");
				$(".zhezhao .xz_shuji li a").removeClass("cur");
				$(".zhezhao .xz_shuji li a:eq(0)").addClass("cur");
				$(".zhezhao .xz_juli li a").removeClass("cur");
				$(".zhezhao .xz_juli li a:eq(0)").addClass("cur");
				$(".xf_xuanze li:eq(0) a").html("全部书籍");
				$(".xf_xuanze li:eq(3) a").html("不限距离");
				loadReleaseByCondition(indexPage);
				return false;
			}
			if(data=="-3"){
				$("#udlsList li.removable").remove();
				xfAlert("此功能要设置活跃地址后才能使用");
				$(".zhezhao .xz_juli li a").removeClass("cur");
				$(".zhezhao .xz_juli li a:eq(0)").addClass("cur");
				$(".xf_xuanze li:eq(3) a").html("不限距离");
				loadReleaseByCondition(indexPage);
				return false;
			}
			var udls = eval('(' + data + ')'); 
			udls = udls.list[0];
			udls = udls["com.opac.readerHome.domain.UdlsRelease"];
			if(indexPage==1){
				$("#udlsList li.removable").remove();
				if(udls == undefined){
					$("#udlsList").append(getNoneDataLi("没有符合条件的转借"));
					return ;
				}
			}
			if(udls == undefined){
//				$("#pullUp").css("display","none");
				
				return ;
			}
			if(udls.length==undefined){
				udls = [udls];
			}
			for(var i=0;i<udls.length;i++){
				var title = udls[i].title;
				var author = udls[i].author;
				if(author == undefined){
					author = "";
				}
				var publisher = udls[i].publisher;
				if(publisher == undefined){
					publisher = "";
				}
				var gLibname = udls[i].gLibname;
				var address = udls[i].district+udls[i].detail;
				var isbn = udls[i].isbn;
				var id=udls[i].id;
				var $li="<li lng='"+udls[i].longitude+"' lat='"+udls[i].latitude+"' phone='"+udls[i].phone+"' qq='"+udls[i].qq+"' userNickname='"+udls[i].userNickname+"' userid='"+udls[i].userid+"' title='"+title+"' author='"+author+"' publisher='"+publisher+"' gLibname='"+gLibname+"' address='"+address+"' isbn='"+isbn+"' id='"+id+"' class='releaseLi removable'><a index='"+i+"' class='toBookDetail' href='#zjxq'><img src='images/default1.jpg' isbn='"+isbn+"'><h3 class='noNewLine'>"+title+"</h3><p>"+author+"  </p><p><span>"+address+"</span><span></span></p></a></li>";
				$("#udlsList").append($li);
			}
			$(".releaseLi").unbind("click");
			$(".releaseLi").click(function(){
				releaseDetail($(this));
			});
			coverCover();
			if(myScroll!=null){
				myScroll.refresh();
			}
		}
	});
}
function loadReleaseByKeyWord(){
	var keyword = $(".sosuo input").val();
	$.ajax({
		url :interfaceMobileUrl+"/getUdls.json?query="+keyword,
		jsonp: "callback",
	    dataType: "jsonp",
		error:function(){
			xfAlert("您的网络环境似乎不稳定");
		},
		success:function(data){
			var udls = eval('(' + data + ')'); 
			udls = udls.list[0];
			udls = udls["com.opac.readerHome.domain.UdlsRelease"];
			$(".shijilieb.tsbgul").html("");
			if(udls == undefined){
				$(".shijilieb.tsbgul").append(getNoneDataLi("没有符合条件的书籍"));
				return ;
			}
			if(udls.length==undefined){
				udls = [udls];
			}
			for(var i=0;i<udls.length;i++){
				var title = udls[i].title;
				if(udls[i].author == undefined){
					var author = '作者信息缺失';
				}else{
					var author = udls[i].author.replace('著','');
				}
				var publisher = udls[i].publisher;
				var gLibname = udls[i].gLibname;
				var address = udls[i].district+udls[i].detail;
				var isbn = udls[i].isbn;
				var id=udls[i].id;
				var $li="<li lng='"+udls[i].longitude+"' lat='"+udls[i].latitude+"' phone='"+udls[i].phone+"' qq='"+udls[i].qq+"' userNickname='"+udls[i].userNickname+"' userid='"+udls[i].userid+"' title='"+title+"' author='"+author+"' publisher='"+publisher+"' gLibname='"+gLibname+"' address='"+address+"' isbn='"+isbn+"' id='"+id+"' class='releaseLi removable'><a index='"+i+"' class='toBookDetail' href='#zjxq'><img src='images/default1.jpg' isbn='"+isbn+"'><h3 class='noNewLine'>"+title+"</h3><p>"+author+" <span>著</span> </p><p><span>"+address+"</span><span></span></p></a></li>";
				$(".shijilieb.tsbgul").append($li);
			}
			$(".releaseLi").unbind("click");
			$(".releaseLi").click(function(){
				releaseDetail($(this));
			});
			coverCover();
		}
	});
}

function loadKinds(){
	$.ajax({
		url :interfaceMobileUrl+"/getAllKinds.json",
		jsonp: "callback",
	    dataType: "jsonp",
		error:function(){
			xfAlert("是不是网络不通啊");
		},
		success:function(data){
			data = eval('(' + data + ')'); 
			data = data.list[0].string;
			if(typeof(data)=="string"){
				data=[data];
			}
			for(var i = 0;i<data.length;i++){
				$(".zhezhao .xz_fenlei").append("<li><a href='#'>"+data[i]+"</a></li>");
			}
			$(".zhezhao .xz_fenlei li a").unbind("click");
			$(".zhezhao .xz_fenlei li a").click(function(){
				indexPage=1;
				$(".zhezhao .xz_fenlei li a").removeClass("cur");
				$(this).addClass("cur");
				$(".xf_xuanze li:eq(1) a").html($(this).html());
				loadReleaseByCondition(indexPage);
			});
		}
	});
}
function loadDistricts(){
	$.ajax({
		url :interfaceMobileUrl+"/getAllDistricts.json?city="+city,
		jsonp: "callback",
		dataType: "jsonp",
		error:function(){
			xfAlert("是不是网络不通啊");
		},
		success:function(data){
			data = eval('(' + data + ')'); 
			data = data.list[0].string;
			if(typeof(data)=="string"){
				data=[data];
			}
			for(var i = 0;i<data.length;i++){
				$(".zhezhao .xz_diqu").append("<li><a href='#'>"+data[i]+"</a></li>");
			}
			
			$(".zhezhao .xz_diqu li a").unbind("click");
			$(".zhezhao .xz_diqu li a").click(function(){
				indexPage=1;
				$(".xf_xuanze li:eq(2) a").html($(this).html());
				$(".zhezhao .xz_diqu li a").removeClass("cur");
				$(this).addClass("cur");
				loadReleaseByCondition(indexPage);
			});
		}
	});
}



